package com.business.action.admin.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.boot.bean.DataTypeBean;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.bean.FieldBean;
import net.sf.rose.jdbc.bean.TableBean;
import net.sf.rose.jdbc.service.MetaService;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.StringUtil;

@Controller
@RequestMapping("/admin/table")
public class TableBeanAction {

	/**
	 * 列出所有表结构信息
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean list(HttpServletRequest request) {
		PageBean page = new PageBean();
		page.setData(Service.getMeta().getTableList());
		return page;
	}

	/**
	 * 保存单个表结构信息
	 */
	@ResponseBody
	@RequestMapping("/saveTable.do")
	public PageBean saveTable(HttpServletRequest request, TableBean table, String json) {
		PageBean page = new PageBean();
		// 处理字段
		List<FieldBean> fields = StringUtil.parseJsonArray(FieldBean.class, json);
		Map<String, DataTypeBean> types = table.getDataType();
		for (FieldBean field : fields) {
			DataTypeBean type = types.get(field.getType());
			field.setJavaType(type.getJavaType());
			field.setDbType(type.getDbType());
		}
		// 处理表
		List<TableBean> list = Service.getMeta().getTableList();
		for (TableBean bean : list) {
			if (bean.getTableName().equals(table.getTableName())) {
				bean.setDatabaseName(table.getDatabaseName());
				bean.setTableName(table.getTableName());
				bean.setViewSql(table.getViewSql());
				bean.setDescription(table.getDescription());
				bean.setClassName(table.getClassName());
				// 处理主键
				for (FieldBean field : fields) {
					// 主键生成器保持不变
					if (field.isPk()) {
						KeyGenerator okg = bean.getPkField().getBeanKeyGenerator();
						okg.setkeyGenerateString(field.getPolicy());
						field.setBeanKeyGenerator(okg);
					}
				}
				bean.setFields(fields);
				return page;
			}
		}
		table.setFields(fields);
		list.add(table);
		return page;
	}

	/**
	 * 删除单个表结构信息
	 */
	@ResponseBody
	@RequestMapping("/deleteTable.do")
	public PageBean deleteTable(HttpServletRequest request, TableBean table) {
		PageBean page = new PageBean();
		List<TableBean> list = Service.getMeta().getTableList();
		for (int i = 0; i < list.size(); i++) {
			TableBean bean = list.get(i);
			if (bean.getTableName().equals(table.getTableName())) {
				list.remove(i);
				return page;
			}
		}
		return page;
	}

	/**
	 * 保存全部表结构
	 */
	@ResponseBody
	@RequestMapping("/saveAll.do")
	public PageBean saveAll(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MetaService ms = Service.getMeta();
		ms.saveAllTableBean();
		ms.initialTableDefine(service);
		return page;
	}

	/**
	 * 更新数据库表结构
	 */
	@ResponseBody
	@RequestMapping("/getUpdateSql.do")
	public PageBean getUpdateSql(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		Map<String, List<String>> map = Service.getMeta().getUpdateTableSql(service);
		Map<String,String> data = new HashMap<>();
		for (String key : map.keySet()) {
			StringBuilder sql = new StringBuilder();
			for (String str : map.get(key)) {
				sql.append(str);// .append(';');
			}
			data.put(key, sql.toString());
		}
		page.setData(data);
		return page;
	}

	/**
	 * 更新数据库表结构
	 */
	@ResponseBody
	@RequestMapping("/updateDatabase.do")
	public PageBean updateDatabase(HttpServletRequest request, Service service, String databaseName, String sql) {
		PageBean page = new PageBean();
		service.setCurrentDatabaseName(databaseName);
		List<String> list = new ArrayList<>();
		String[] arr = sql.split(";");
		for (String s : arr) {
			s = s.trim();
			if (!s.equals("")) {
				list.add(s);
			}
		}
		Service.getMeta().executeUpdateTableSql(service, list);
		service.commit();
		return page;
	}

}
